package org.item.product.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.item.common.bean.Product;
import org.item.common.service.IProductService;
import org.item.product.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;
@Service
public class ProductService implements IProductService {
   @Autowired
    ProductMapper productMapper;

    @Override
    public Product insert(Product product) {
        productMapper.insert(product);
        Product result=this.getById(product.getId());
        return result;
    }

    @Override
    public Product update(Product product) {
        productMapper.updateById(product);
        Product result=this.getById(product.getId());
        return result;
    }

    @Override
    public int delete(Integer id) {
        int num=productMapper.deleteById(id);;
        return num;
    }

    @Override
    public Product getById(Integer id) {
        Product result=productMapper.selectById(id);
        return result;
    }

    @Override
    public IPage<Product> getListByPage(IPage page, Product product) {
        Wrapper wrapper=new QueryWrapper(product);
        IPage result= productMapper.selectPage(page,wrapper);
        return result;
    }

    @Override
    public List<Product> getList(Product product) {
        Wrapper wrapper=new QueryWrapper(product);
        List<Product> result=productMapper.selectList(wrapper);
        return result;
    }

    @Override
    public int setActive(Product product) {
        UpdateWrapper wrapper=new UpdateWrapper();
        if (product.getIs_active()==0){
            wrapper.set("is_active",1);
        }else {
            wrapper.set("is_active",0);
        }
        wrapper.eq("id",product.getId());
        int result=productMapper.update(wrapper);
        return result;
    }
}
